local E, L, V, P, G, _ = unpack(select(2, ...)); --Inport: Engine, Locales, PrivateDB, ProfileDB, GlobalDB, Localize Underscore
local B = E:GetModule('Bags');

local TOTAL_BAGS = NUM_BAG_FRAMES + 1
local tinsert = table.insert

local function OnEnter()
	if E.db.bags.bagBar.mouseover ~= true then return; end
	UIFrameFadeIn(PandemoniuMUIBags, 0.2, PandemoniuMUIBags:GetAlpha(), 1)
end

local function OnLeave()
	if E.db.bags.bagBar.mouseover ~= true then return; end
	E:UIFrameFadeOut(PandemoniuMUIBags, 0.2, PandemoniuMUIBags:GetAlpha(), 0)
end

function B:SkinBag(bag)
	local icon = _G[bag:GetName().."IconTexture"]
	bag.oldTex = icon:GetTexture()
	
	bag:StripTextures()
	bag:SetTemplate("Default", true)
	bag:StyleButton(true)
	icon:SetTexture(bag.oldTex)
	icon:SetInside()
	icon:SetTexCoord(unpack(E.TexCoords))
end

function B:SizeAndPositionBagBar()
	if not PandemoniuMUIBags then return; end
	if E.db.bags.bagBar.mouseover then
		PandemoniuMUIBags:SetAlpha(0)
	else
		PandemoniuMUIBags:SetAlpha(1)
	end
	
	if E.db.bags.bagBar.showBackdrop then
		PandemoniuMUIBags.backdrop:Show()
	else
		PandemoniuMUIBags.backdrop:Hide()
	end
	
	for i=1, #PandemoniuMUIBags.buttons do
		local button = PandemoniuMUIBags.buttons[i]
		local prevButton = PandemoniuMUIBags.buttons[i-1]
		button:Size(E.db.bags.bagBar.size)
		button:ClearAllPoints()
		if E.db.bags.bagBar.growthDirection == 'HORIZONTAL' and E.db.bags.bagBar.sortDirection == 'ASCENDING' then
			if i == 1 then
				button:SetPoint('LEFT', PandemoniuMUIBags, 'LEFT', E.db.bags.bagBar.spacing, 0)
			elseif prevButton then
				button:SetPoint('LEFT', prevButton, 'RIGHT', E.db.bags.bagBar.spacing, 0)
			end
		elseif E.db.bags.bagBar.growthDirection == 'VERTICAL' and E.db.bags.bagBar.sortDirection == 'ASCENDING' then
			if i == 1 then
				button:SetPoint('TOP', PandemoniuMUIBags, 'TOP', 0, -E.db.bags.bagBar.spacing)
			elseif prevButton then
				button:SetPoint('TOP', prevButton, 'BOTTOM', 0, -E.db.bags.bagBar.spacing)
			end		
		elseif E.db.bags.bagBar.growthDirection == 'HORIZONTAL' and E.db.bags.bagBar.sortDirection == 'DESCENDING' then	
			if i == 1 then
				button:SetPoint('RIGHT', PandemoniuMUIBags, 'RIGHT', -E.db.bags.bagBar.spacing, 0)
			elseif prevButton then
				button:SetPoint('RIGHT', prevButton, 'LEFT', -E.db.bags.bagBar.spacing, 0)
			end		
		else
			if i == 1 then
				button:SetPoint('BOTTOM', PandemoniuMUIBags, 'BOTTOM', 0, E.db.bags.bagBar.spacing)
			elseif prevButton then
				button:SetPoint('BOTTOM', prevButton, 'TOP', 0, E.db.bags.bagBar.spacing)
			end			
		end
	end
	
	if E.db.bags.bagBar.growthDirection == 'HORIZONTAL' then
		PandemoniuMUIBags:Width(E.db.bags.bagBar.size*(TOTAL_BAGS) + E.db.bags.bagBar.spacing*(TOTAL_BAGS) + E.db.bags.bagBar.spacing)
		PandemoniuMUIBags:Height(E.db.bags.bagBar.size + E.db.bags.bagBar.spacing*2)	
	else
		PandemoniuMUIBags:Height(E.db.bags.bagBar.size*(TOTAL_BAGS) + E.db.bags.bagBar.spacing*(TOTAL_BAGS) + E.db.bags.bagBar.spacing)
		PandemoniuMUIBags:Width(E.db.bags.bagBar.size + E.db.bags.bagBar.spacing*2)		
	end
end

function B:LoadBagBar()
	if not E.private.bags.bagBar then 
		return 
	end
	
	local PandemoniuMUIBags = CreateFrame("Frame", "PandemoniuMUIBags", E.UIParent)
	PandemoniuMUIBags:SetPoint('TOPRIGHT', RightChatPanel, 'TOPLEFT', -4, 0)
	PandemoniuMUIBags.buttons = {};
	PandemoniuMUIBags:CreateBackdrop()
	PandemoniuMUIBags.backdrop:SetAllPoints()
	PandemoniuMUIBags:EnableMouse(true)
	PandemoniuMUIBags:SetScript("OnEnter", OnEnter)
	PandemoniuMUIBags:SetScript("OnLeave", OnLeave)

	MainMenuBarBackpackButton:SetParent(PandemoniuMUIBags)
	MainMenuBarBackpackButton.SetParent = E.dummy
	MainMenuBarBackpackButton:ClearAllPoints()	
	MainMenuBarBackpackButtonCount:FontTemplate(nil, 10)
	MainMenuBarBackpackButtonCount:ClearAllPoints()
	MainMenuBarBackpackButtonCount:Point("BOTTOMRIGHT", MainMenuBarBackpackButton, "BOTTOMRIGHT", -1, 4)
	MainMenuBarBackpackButton:HookScript('OnEnter', OnEnter)
	MainMenuBarBackpackButton:HookScript('OnLeave', OnLeave)
		
	tinsert(PandemoniuMUIBags.buttons, MainMenuBarBackpackButton)
	self:SkinBag(MainMenuBarBackpackButton)

	for i=0, NUM_BAG_FRAMES-1 do
		local b = _G["CharacterBag"..i.."Slot"]
		b:SetParent(PandemoniuMUIBags)
		b.SetParent = E.dummy
		b:HookScript('OnEnter', OnEnter)
		b:HookScript('OnLeave', OnLeave)
		
		self:SkinBag(b)
		tinsert(PandemoniuMUIBags.buttons, b)
	end
	
	self:SizeAndPositionBagBar()
	E:CreateMover(PandemoniuMUIBags, 'BagsMover', L['Bags'])
end